(function($){
	
    $(function(){
    	
    	var editor;
		KindEditor.ready(function(K) {
			editor = K.create('textarea[name="memo"]', {
				resizeType : 1,
				allowPreviewEmoticons : false,
				allowImageUpload : false,
				items : [
					'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
					'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
					'insertunorderedlist', '|',  'image', 'link'],
				afterChange:function(){
					K("#memoCount").val(this.count("text"));
					K("#memoHtml").val(this.html());
					K("#memoHtmlCount").html(this.count());
				}
			});
			
		});
		
		//从后台获取数据填充页面
	(function(){
		var hiType = $.trim($("#hiType").val());
		var hiMinitype = $.trim($("#hiMinitype").val());
				if(hiType!==undefined && hiType!==''){
					$('#type').val(hiType);
				}
				if(hiMinitype!==''&&hiMinitype!==undefined){
					$('#minitype').val(hiMinitype);
				}
				
			})();

	$('#awardImageUpload').click(function(){
		var imageFile = $('#awardImage');
		var showUrl = $('#showAward');
		var hiddenUrl = $('#awardImageUrl');
		ajaxUpload(imageFile,showUrl,hiddenUrl);
	});
	
	$('#updateAwardButton').click(function(){
		
		//表单处理
		$('.validate-tips span').remove();
		if($('#memoCount').val()==='0'){
			$('#memoHtml').val('');
		}
		if($('#memoHtml').val().length >2048 ){
			$('.validate-tips').append('<span class="error">' +'请输入小于2048个字（包括格式）的使用说明介绍'+ '</span>');
		}else{
			if($('#awardImageUrl').val()!=null && $('#awardImageUrl').val()!=undefined&&$('#awardImageUrl').val()!=''){
				var flag = $("#updateAwardForm").validate(VALIDATE.addAward).form();
				if(flag){
					$('#updateAwardForm').submit();
					}
			}else{
				$('.validate-tips').append('<span class="error">' +'请选择图片'+ '</span>');
			}
			
		}
		
	});
		
    	
    });
    /*
     * ajax上传图片
     * 
     */
    function ajaxUpload(imageFile,showUrl,hiddenUrl) {
		if(imageFile.val()==""){
			showTips("请选择图片");
			return false;
		}
		var imageid = imageFile.attr('id');
		var activeId = $('#activeId').val();
		$.ajaxFileUpload ({
			url :ctx+'/award/ajaxUpload.do?activeId='+activeId,
			secureuri :false,
			fileElementId :imageid,
			dataType : 'text',
			success : function (data, status){
				if(data.startWith("error")){
					showTips(data);
				}else{
					showUrl.attr('src',data);
					hiddenUrl.val(data);
					imageFile.attr("id",imageid);
				}
			},
			error: function (data, status, e){
				showTips(status);
			}
		})
		return false;
	}// end ajaxUpload

})(jQuery);